package com.wangwenjun.concurrency.chapter17;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class TaoBaoTest4 {


    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();


        new Thread(()->{
            lock.lock();
            try{
                try {
                    condition.await(); //阻塞当前线程并释放锁
                    System.out.println(Thread.currentThread().getName()+" go on");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }finally {
                System.out.println(Thread.currentThread().getName()+ " unlock");
                lock.unlock();
            }

        },"t1").start();


        TimeUnit.SECONDS.sleep(1);

        new Thread(()->{
            lock.lock();
            try {
                condition.signal();// 不会释放锁需要配合unlock使用
            } finally {
                System.out.println(Thread.currentThread().getName()+ " unlock");
                lock.unlock();
            }
        }, "t2").start();

    }
}
